package com.orphan.audit.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orphan.audit.dao.DepositMapper;
import com.orphan.audit.po.Deposit;
import com.orphan.audit.po.OrderCheck;
import com.orphan.audit.dao.OrderCheckMapper;
import com.orphan.audit.service.IDepositService;
import com.orphan.audit.service.IOrderCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orphan.audit.service.IPreDiscountService;
import com.orphan.util.dto.OrderDto;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wdn
 * @since 2021-07-15
 */
@Service
public class OrderCheckServiceImpl extends ServiceImpl<OrderCheckMapper, OrderCheck> implements IOrderCheckService {

    @Resource
    private OrderCheckMapper orderCheckMapper;
    @Resource
    private IDepositService depositService;
    @Resource
    private IPreDiscountService preDiscountService;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public List<OrderCheck> findAll() {
        return orderCheckMapper.findAll();
    }

    @Override
    public PageInfo<OrderCheck> getAll(int pageNO) {
        PageHelper.startPage(pageNO, 3);
        PageInfo pageInfo = PageInfo.of(orderCheckMapper.findAll());
        return pageInfo;
    }

    @RabbitListener(queues = "settlementQueue")
    public void getOrderMQ(OrderDto dto){
        OrderCheck orderCheck = null;
        orderCheck.setTOrderid(dto.getOrderId());
        orderCheck.setTTotalmoney(dto.getTotalAmount());
        orderCheck.setTStatus(2);
        orderCheck.setTPay(dto.getPayMethod());
        orderCheck.setTTime(LocalDateTime.now());
        orderCheck.setTStuff("张三");
        //如果支付方式为预付款，进行预付款金额判断
        if(dto.getPayMethod() == 4) {
            //通过用户id查询余额，判断能否购买
            long id = dto.getUserId();
            Deposit deposit = depositService.queryDeposit(id);
            //判断此用户有无预存款
            if(deposit == null) {
                System.out.println("没有预存款你买锤子");
                dto.setOrderStatus(4);
                orderCheck.setTStatus(4);
            }else {
                //如果该用户有预存款，获得其折扣率
                float rate = preDiscountService.getRate(deposit.getTTotal());
                //判断，如果总金额*折扣率=实付金额小于存款，则购买成功(2)，反之失败(4)。
                if(dto.getTotalAmount() * rate < deposit.getTBalance()) {
                    //购买成功后余额扣款
                    deposit.setTBalance(deposit.getTBalance() - dto.getTotalAmount() * rate);
                    depositService.update(deposit);
                    dto.setOrderStatus(2);
                }else {
                    dto.setOrderStatus(4);
                }
            }
        }
        //此操作保存至购物订单数据库
        orderCheckMapper.insert(orderCheck);
        Map<String,Object> map = null;
        map.put("orderId",dto.getOrderId());
        map.put("payStatus",dto.getOrderStatus());
        rabbitTemplate.convertAndSend("SettlementBackDirectExchange","SettlementBackMessage",map);


    }
}
